// pages/my/call-record/call-record.js
const httpUtils = require('../../../utils/httpUtil')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    callRecordList: [],
    isRefresh: true,
    pageNum: 1,
    pageSize: 50,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryCallRecord(this.data.pageNum, this.data.pageSize, false);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      isRefresh: true,
      pageNum: 1,
      hasMore: true
    });
    this.queryCallRecord(this.data.pageNum, this.data.pageSize, false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.queryCallRecord(this.data.pageNum, this.data.pageSize, true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  queryCallRecord(pageNum, pageSize, loadMore) {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/shout/shout/list?pageNum='+pageNum+'&pageSize='+pageSize,
    }).then(res=>{
      if (res.data.code == 200) {
        if (res.data.rows.length > 0) {
          this.setData({
            pageNum: this.data.pageNum + 1,
            callRecordList: loadMore ? this.data.callRecordList.concat(res.data.rows) : res.data.rows,
          })
        } else {
          this.setData({
            hasMore: false
          });
        }
      }
      
      this.setData({
        isRefresh: false,
      });
    })
  },

  jump2Reply(e) {
    const index = e.currentTarget.dataset.index;
    const target = this.data.callRecordList[index];

    const url = '/pages/call/answer/answer?callId='+target.callId+'&callType=0&callContent='+target.recordContent+'&history=true';
    console.log('跳转url：'+url);
    wx.navigateTo({
      url: url,
    });
  }
})